home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 016a / rgrep.zip / RGREP.BAT < prev   
DOS Batch File  |  1991-10-09  |  7KB  |  218 lines

  1. @REM=(qq!
  2. @perl286 %0.bat %1 %2 %3 %4 %5 %6 %7 %8 %9
  3. @goto end !) if 0 ;
  4. #
  5. # MS-DOS batch header and footer material added by Duane Paulson.
  6. #
  7. #  Note that the compressed file feature mentioned below refers to the Unix
  8. #  compression program. Rgrep, _as written_, will not handle such things as
  9. #  ZIP or ARC files.
  10. #
  11. # ------------------------------------------------------------------------
  12. # Path: tut.cis.ohio-state.edu!pacific.mps.ohio-state.edu!zaphod.mps.ohio-state.edu!sdd.hp.com!decwrl!uunet!mcsun!hp4nl!ruuinf!piet
  13. # From: piet@cs.ruu.nl (Piet van Oostrum)
  14. # Newsgroups: comp.lang.perl
  15. # Subject: Recursive grep in perl
  16. # Message-ID: <3985@ruuinf.cs.ruu.nl>
  17. # Date: 8 Oct 90 17:03:21 GMT
  18. # Sender: news@ruuinf.cs.ruu.nl
  19. # Reply-To: piet@cs.ruu.nl (Piet van Oostrum)
  20. # Organization: Dept of Computer Science, Utrecht University, The Netherlands
  21. # Lines: 191
  22. #
  23. # This is my 'recursive grep'. At first sight it is a "find -exec egrep"
  24. # replacement. Actually it does things that can't be done with the find/egrep
  25. # combination:
  26. #   It silently skips binary files (unless told not to)
  27. #   It uncompresses compressed file while scanning
  28. #   It allows / in filename patters e.g. search in all files src/*.c
  29. #   It allows real perl regexps for filename patterns
  30. # ------------------------------------------------------------------------
  31.  
  32.  
  33.  
  34. die "Usage: rgrep [-iredblL] regexp filepat ...\n       rgrep -h for help\n"
  35.     if $#ARGV < $[;
  36.  
  37. # Written by Piet van Oostrum <piet@cs.ruu.nl>
  38. # This is really free software
  39.  
  40. $nextopt = 1;
  41. $igncase = '';
  42. $regpat = 0;
  43. $links = 0;
  44. $error = 0;
  45. $skipbin = 1;
  46. $debug = 0;
  47.  
  48. do { $regexp = shift (@ARGV); } while &checkopt ($regexp);
  49. $icreg = $igncase;
  50. $igncase = '';
  51.  
  52. eval 'sub grep_file {
  53.         while (<F>) {
  54.         $ln++;
  55.         if (/$regexp/o' . $icreg .') {
  56.             print "$file:$ln:$_";
  57.             print "\n" if substr($_, -1, 1) ne "\n";
  58.         }
  59.         }
  60. }';
  61.  
  62. for (@ARGV) {
  63.     if (! &checkopt ($_)) {
  64.         if ($igncase || $regpat || /[?*[]/ || ! -e) {
  65.         if ($regpat) {
  66.         s/#/\\#/g;
  67.         $_ = "#$_#";
  68.         } else { # translate File pattern into regexp
  69.                 $re = '#($|/)'; $save = $_;
  70.             while (/[[*?+()|.^$#]/) {
  71.             $re .= $`;
  72.             $c = $&;
  73.             $_ = $';
  74.             if ($c eq '*') { $c = '[^/]*'; }
  75.             elsif ($c eq '?') { $c = '[^/]'; }
  76.             elsif ($c eq '[') {
  77.                 if (/.\]/) { $c = "[$`$&"; $_ = $'; }
  78.             else {
  79.                 $error++;
  80.                 printf stderr "Illegal filepattern %s\n", $save;
  81.             }
  82.             } else { $c = "\\$c"; }
  83.             $re .= $c;
  84.         }
  85.         $_ = "$re$_\$#$igncase";
  86.         }
  87.         print "filepat: $_\n" if $debug;
  88.             push (@filepat, $_);
  89.     }
  90.         else { push (@files, $_); print "file: $_\n" if $debug; }
  91.     }
  92. }
  93.  
  94. exit 1 if $errors ;
  95.  
  96. if ($#filepat < $[) {
  97.     eval "sub in_pat {1;}" ;
  98. }
  99. else {
  100.     $subtxt = 'sub in_pat { local ($f) = @_;';
  101.     $or = "";
  102.     for (@filepat) {
  103.     $subtxt .= $or . '$f =~ m' . $_;
  104.     $or = " || ";
  105.     }
  106.     $subtxt .= ';};1';
  107.  
  108.     if (! eval $subtxt) {
  109.         print $@;
  110.         exit 1;
  111.     }
  112.  
  113. @files = (".") if $#files < $[;
  114.  
  115. for $file (@files) {
  116.     &do_grep ($file);
  117. }
  118.  
  119. sub do_grep {
  120.     local ($file) = @_;
  121.     local (*F, $ln, $f, $g, @dirfiles);
  122.     if (-f $file) {
  123.     if (open (F, $file)) {
  124.         if (-B F) { # binary file --  may be compressed/compacted
  125.         if (($cx1 = getc(F)) eq "\377" && (getc(F) eq "\037")) {
  126.             open (F, "uncompact < $file|");
  127.             if ($skipbin && -B F) { close (F); return; }
  128.         }
  129.         elsif ($cx1 eq "\037" && (getc(F) eq "\235")) {
  130.             open (F, "uncompress < $file|");
  131.             if ($skipbin && -B F) { close (F); return; }
  132.         }
  133.         elsif ($skipbin) {
  134.             close (F); return;
  135.         }
  136.         }
  137.         print "Reading $file\n" if $debug;
  138.         &grep_file;
  139.     } else {
  140.         print stderr "Cannot open $file\n";
  141.     }
  142.     }
  143.     elsif (-d $file) {
  144.     print "Entering $file\n" if $debug;
  145.     if (opendir (F, $file)) {
  146.         @dirfiles = readdir (F);
  147.         closedir (F);
  148.         for $f (@dirfiles) {
  149.         next if ($f eq '.' || $f eq '..');
  150.         $g = "$file/$f";
  151.         next if (-l $g && ($links < 1 || $links == 1 && -d $g));
  152.         if (-f $g && &in_pat ($g) || -d _) {
  153.             &do_grep ($g);
  154.         }
  155.         }
  156.     } else {
  157.         print stderr "Can't open $file\n";
  158.     }
  159.     }
  160. }
  161.  
  162. sub checkopt {
  163.     local ($_) = $_[0];
  164.     if (/^-/ && $nextopt) {
  165.         $nextopt = 1;
  166.     @opt = split (/-*/,$_); shift (@opt);
  167.         for $opt (@opt) {
  168.         if ($opt eq 'i') { $igncase = 'i'; }
  169.         elsif ($opt eq 'd') { $debug = 1; }
  170.         elsif ($opt eq 'l') { $links = 1; }
  171.         elsif ($opt eq 'L') { $links = 2; }
  172.         elsif ($opt eq 'b') { $skipbin = 0; }
  173.         elsif ($opt eq 'r') { $regpat = 1; }
  174.         elsif ($opt eq 'e') { $nextopt = 0; }
  175.         elsif ($opt eq 'h' || $opt eq 'H') { & help; }
  176.         else { $error++; printf stderr "Unknown option -%s\n", $opt; }
  177.     }
  178.         return 1;
  179.     }
  180.     $nextopt = 1;
  181.     return 0;
  182. }
  183.  
  184. sub help {
  185.     print <<'HELP'; exit 0;
  186. Usage: rgrep [-iredblL] regexp filepat ...
  187.   regexp = perl regular expression to search
  188.   filepat ... = a list of files and directories to be searched or
  189.       file patterns to match filenames.
  190.       filepat will be interpreted as file or directory name if it exists
  191.       as such, and does not contain the metacharacters [ ] ? or *. After
  192.       the options -i and -r all filepats will be considered patterns.
  193.       rgrep will search all files in any of the directories given (and its
  194.       subdirectories) that match any of the filepats, except binary files.
  195.       Compressed files will be searched in uncompressed form.
  196.       Note: filepats may contain / contrary to find usage.
  197.   -b  Don't skip binary files.
  198.   -i  Ignore case, either in the regexp or in filename matching (depending
  199.       on the location). Before the regexp only applies to the regexp,
  200.       otherwise to the filepats following it.
  201.   -r  The following filepats are treated as real perl regexps rather than
  202.       shell style filename patterns. In this case / is not a special
  203.       character, i.e. it is matched by . and matching is not anchored (you
  204.       must supply ^ and $ yourself). E.g. a.b matches the file /xa/by/zz.
  205.   -l  Do follow symbolic links only for files (default is do not follow).
  206.   -L  Do follow symbolic links for files and directories.
  207.   -e  Do not interpret following argument as option. Useful if regexp or
  208.       filepat starts with a -.
  209.   -d  Debugging: Give a lot of output on what happens.
  210.   -h  print this message and exit.
  211. Piet van Oostrum <piet@cs.ruu.nl>
  212. HELP
  213. }
  214.  
  215. @REM=(qq!
  216. :end !) if 0 ;
  217.